﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Input;
using System.Windows;

namespace Passwords.ViewModels
{
    public class ViewModelBase : INotifyPropertyChanged
    {
        private List<CommandBinding> bindings = new List<CommandBinding>();

        protected void BindCommand(ICommand command, ExecutedRoutedEventHandler executed, CanExecuteRoutedEventHandler canExecute = null)
        {
            CommandBinding binding = canExecute != null ? new CommandBinding(command, executed, canExecute) : new CommandBinding(command, executed);

            Window w = App.Current.MainWindow;
            w.CommandBindings.Add(binding);
            bindings.Add(binding);
        }

        protected void BindCommand(CommandBinding binding)
        {
            Window w = App.Current.MainWindow;
            w.CommandBindings.Add(binding);
            bindings.Add(binding);
        }

        protected void UnbindCommands()
        {
            Window w = App.Current != null ? App.Current.MainWindow : null;
            if (w != null)
            {
                foreach (CommandBinding b in bindings)
                {
                    w.CommandBindings.Remove(b);
                }
            }
        }



        protected  virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler eh = PropertyChanged;
            if (null != eh) eh(this, new PropertyChangedEventArgs(propertyName));
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
